<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px" class="compact-form">
      <el-form-item label="配送员ID" prop="riderId" class="form-item-compact" >
        <el-input
            v-model="queryParams.riderId"
            placeholder="请输入配送员ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="reassigned" class="form-item-compact" >
        <el-input
            v-model="queryParams.reassigned"
            placeholder="请输入改派单量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="配送费" prop="deliveryFee" class="form-item-compact">
        <el-input
            v-model="queryParams.deliveryFee"
            placeholder="请输入配送费(单位：P)"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item class="form-item-compact">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-button type="success" icon="DataAnalysis" @click="goToVisualization">数据分析</el-button>

    <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="配送员ID" align="center" prop="riderId" />
      <el-table-column label="配送员姓名" align="center" prop="rider.name" width="100"/>
      <el-table-column label="配送员手机号" align="center" prop="rider.phone" width="160"/>
      <el-table-column label="持单量" align="center" prop="currentOrders" />
      <el-table-column label="抢单量" align="center" prop="grabbedOrders" />
      <el-table-column label="系统派单" align="center" prop="systemAssigned" />
      <el-table-column label="增派单量" align="center" prop="additionalAssigned" />
      <el-table-column label="改派单量" align="center" prop="reassigned" />
      <el-table-column label="完成单量" align="center" prop="completedOrders" />
      <el-table-column label="配送费(单位：P)" align="center" prop="deliveryFee" width="160"/>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="120">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改配送员绩效统计对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="statisticsRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Statistics">
import { listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics } from "@/api/system/statistics"
import { useRouter } from 'vue-router'  // 添加这行

const { proxy } = getCurrentInstance()
const router = useRouter()

const statisticsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    riderId: null,
    currentOrders: null,
    grabbedOrders: null,
    systemAssigned: null,
    additionalAssigned: null,
    reassigned: null,
    completedOrders: null,
    deliveryFee: null,
    updatedAt: null
  },
  rules: {
    riderId: [
      { required: true, message: "配送员ID不能为空", trigger: "blur" }
    ],
    currentOrders: [
      { required: true, message: "当前持单量不能为空", trigger: "blur" }
    ],
    grabbedOrders: [
      { required: true, message: "抢单量不能为空", trigger: "blur" }
    ],
    systemAssigned: [
      { required: true, message: "系统派单量不能为空", trigger: "blur" }
    ],
    additionalAssigned: [
      { required: true, message: "增派单量不能为空", trigger: "blur" }
    ],
    reassigned: [
      { required: true, message: "改派单量不能为空", trigger: "blur" }
    ],
    completedOrders: [
      { required: true, message: "完成单量不能为空", trigger: "blur" }
    ],
    deliveryFee: [
      { required: true, message: "配送费(单位：P)不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询配送员绩效统计列表 */
function getList() {
  loading.value = true
  listStatistics(queryParams.value).then(response => {
    statisticsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    riderId: null,
    currentOrders: null,
    grabbedOrders: null,
    systemAssigned: null,
    additionalAssigned: null,
    reassigned: null,
    completedOrders: null,
    deliveryFee: null,
    updatedAt: null
  }
  proxy.resetForm("statisticsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加配送员绩效统计"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getStatistics(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改配送员绩效统计"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["statisticsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStatistics(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addStatistics(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 跳转到可视化页面 */
function goToVisualization() {
  router.push('/manage/visualization')
}

getList()



</script>

